package com.keepsport.keepsportapi.service.impl;

import com.keepsport.keepsportapi.config.RedisKeyConfig;
import com.keepsport.keepsportapi.dao.SmsLogDao;
import com.keepsport.keepsportapi.entity.SmsLog;
import com.keepsport.keepsportapi.service.intf.SmsLogService;
import com.keepsport.keepsportapi.type.SmsLogType;
import com.keepsport.keepsportapi.utils.AliSmsUtil;
import com.keepsport.keepsportapi.utils.JedisUtil;
import com.keepsport.keepsportapi.utils.NumUtil;
import com.keepsport.keepsportapi.utils.StringUtil;
import com.keepsport.keepsportapi.vo.R;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

/**
 * 　　　　　   　▍ ★∴
 * 　　　．．．．▍▍．..．█▍ ☆ ★∵ ..../
 * 　　 　◥█▅▅██▅▅██▅▅▅▅▅▅▅▅▅███◤
 * 　　 　．◥███████████████◤
 * 　 ～～～～◥█████████████◤～～～～
 *
 * @author dyb
 * @create 2022-06-29 17:13
 */
@Service
public class SmsLogServiceImpl implements SmsLogService {
    @Resource
    private SmsLogDao logDao;
    @Override
    public R sendLoginCode(String phone) {
        //1.校验
        if(StringUtil.isNotEmpty(phone)){
            //2.生成验证码
            String num= NumUtil.createCode(6)+"";

            //3.发送验证码
            if(AliSmsUtil.sendSmsCode(phone,num,AliSmsUtil.SMS_TEM_LOGIN)){

                //4.记录验证码 到Redis
                JedisUtil.addString(RedisKeyConfig.SMS_CODE_LOGIN+phone,num,RedisKeyConfig.SMS_CODE_TIME);

                //添加数据到表
                logDao.insert(new SmsLog(phone, SmsLogType.登录验证码.getValue(),"完成登录验证码发送！",1));

                return R.ok();
            }else {
                //添加数据到表
                logDao.insert(new SmsLog(phone, SmsLogType.登录验证码.getValue(),"登录验证码发送失败！",2));
            }
        }
        return R.fail();
    }

    @Override
    public R sendFindCode(String phone) {
        //1.校验
        if (StringUtil.isNotEmpty(phone)) {
            //2.生成验证码
            String num = NumUtil.createCode(6) + "";

            //3.发送验证码
            if (AliSmsUtil.sendSmsCode(phone, num, AliSmsUtil.SMS_TEM_FIND)) {

                //4.记录验证码 到Redis
                JedisUtil.addString(RedisKeyConfig.SMS_CODE_FIND + phone, num, RedisKeyConfig.SMS_CODE_TIME);

                //添加数据到表
                logDao.insert(new SmsLog(phone, SmsLogType.密码找回.getValue(), "完成密码找回验证码发送！", 1));

                return R.ok();
            } else {
                //添加数据到表
                logDao.insert(new SmsLog(phone, SmsLogType.密码找回.getValue(), "登录密码找回发送失败！", 2));
            }
        }
        return R.fail();
    }
}
